"""
This file is part of GSBEdit. For copyright and licensing information, please
see gsbedit.py in the GSBEdit base directory.
"""


import wx

import ui_elements.controls.module_class_tree as mct
import app.module_config as mc


MW_CANCEL = 5000
MW_CREATE = 5001


class ModuleWizard(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, -1)
        
        self.configs = mc.ModuleConfig('data/modules.cfg')
        self.parent = parent
        
        self.module_name = wx.TextCtrl(self, -1)
        create_button = wx.Button(self, MW_CREATE, 'Create module')
        cancel_button = wx.Button(self, MW_CANCEL, 'Cancel')
        self.class_tree = mct.ModuleClassTree(self)
        
        main_sizer = wx.BoxSizer(wx.VERTICAL)

        main_sizer.Add(wx.StaticText(self, -1, 'Give the module a name:'), 0, wx.EXPAND)
        main_sizer.Add((0, 5), 0, wx.EXPAND)
        main_sizer.Add(self.module_name, 0, wx.EXPAND)
        main_sizer.Add((0, 10), 0, wx.EXPAND)

        main_sizer.Add(wx.StaticText(self, -1, 'Select module class:'), 0, wx.EXPAND)
        main_sizer.Add((0, 5), 0, wx.EXPAND)
        main_sizer.Add(self.class_tree, 1, wx.EXPAND)
        main_sizer.Add((0, 10), 0, wx.EXPAND)
        
        #main_sizer.SetItemMinSize(self.class_tree, (-1, 400))
        
        buttons = wx.BoxSizer(wx.HORIZONTAL)
        buttons.Add(create_button, 2, wx.EXPAND)
        buttons.Add((0, 0), 1, wx.EXPAND)
        buttons.Add(cancel_button, 2, wx.EXPAND)
        
        main_sizer.Add(buttons, 0, wx.EXPAND)
        
        self.Bind(wx.EVT_BUTTON, self.cancel, id=MW_CANCEL)
        self.Bind(wx.EVT_BUTTON, self.create_module, id=MW_CREATE)

        outer = wx.BoxSizer(wx.VERTICAL)        
        outer.Add(main_sizer, 1, wx.EXPAND | wx.ALL, 10)
        
        self.SetAutoLayout(True)
        self.SetSizer(outer)
        self.Layout()
    
        
    def cancel(self, event):
        self.parent.close(None)
    
    
    def create_module(self, event):
        name = self.module_name.GetValue()
        class_name = self.class_tree.get_selected_class()
        self.parent.create_module(name, class_name)
        self.parent.close(None)


class ModuleWizardWindow(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, -1, 'Module Creation Wizard', size=(300, 350))
        
        self.parent = parent        
        
        self.Bind(wx.EVT_CLOSE, self.close)
        
        self.panel = ModuleWizard(self)
        
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.panel, 1, wx.EXPAND)

        self.SetAutoLayout(True)
        self.SetSizer(sizer)
        self.Layout()
        
        self.Center()
        self.Show(True)
        self.Raise()
    
    
    def create_module(self, name, class_name):
        self.parent.create_module(name, class_name)
    
    
    def close(self, event):
        self.Destroy()
